<template>
	<view>

		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">账号注册</block>
		</cu-custom>
		<view class="cu-form-group margin-top page-top">
			<view class="title">昵称:</view>
			<input placeholder="昵称一但注册无法变更" v-model="form.nickname"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">账号:</view>
			<input placeholder="唯一账号可用于登录" v-model="form.username"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">邮箱地址:</view>
			<input placeholder="唯一邮箱号可用于登录" v-model="form.email"></input>
			<button class='cu-btn bg-green shadow' @click="sendEmail()">
				{{codeTime == 0?"发送验证": "倒计时："+codeTime}}
			</button>
		</view>
		<view class="cu-form-group">
			<view class="title">邮箱验证码:</view>
			<input placeholder="发送验证后再邮箱中进行查看" v-model="form.emailCode"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">输入密码: </view>
			<input type="password" placeholder="输入密码" v-model="form.password"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">确认密码: </view>
			<input type="password" placeholder="二次确认密码" v-model="form.passwordTwo"></input>
		</view>
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-blue lg" @click="submit()">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {},
				codeTime: 0
			}
		},
		methods: {
			submit() {
				if(this.form.password !== this.form.passwordTwo){
					uni.showToast({
						title: "两次输入的密码不一致",
						icon: 'none'
					})
					return;
				}
				this.crud.post(this.path, "/api/client/all/user/register", this.form).then(res => {
					if (res.data.code == 200) {
						// uni.showToast({
						// 	title: "注册成功,请返回个人中心进行登录",
						// 	icon: 'none'
						// })
						uni.navigateTo({
							url: "/pages/fpage4/user/login",
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					}
				})
			},
			sendEmail() {
				if (this.codeTime > 0) {
					uni.showToast({
						title: "30秒内不能重复d点击发送哦",
						icon: 'none'
					})
				} else {
					//使用方法名字执行方法
					this.crud.post(this.path, "/api/client/all/user/sendEmailAuthCode?email=" + this.form.email).then(res => {
						if (res.data.code == 200) {
							uni.showToast({
								title: "发送成功",
								icon: 'none'
							})
							
							this.codeTime = 30;
							this.sendCode(this);
						}
					})
				}
			},
			//定时器 异步运行  
			sendCode(ths) {
				setInterval(function() {
					if (ths.codeTime > 0) {
						ths.codeTime--;
						ths.sendCode(ths.codeTime);
					}
				}, 1000)
			},
		},
	}
</script>

<style>

</style>
